<?php 

namespace Weixin\Controller;
use Common\Controller\WeixinBase;
use Common\Model\Model;

class IndexController extends WeixinbaseController
{
	
	public function index()
	{
	    //中间广告位
	    $this->assign('indexmidad', D('Weixin/Index')->getAdvert(1,0,5));
	    //左侧广告位
	    $this->assign('indexleftad', D('Weixin/Index')->getAdvert(2,0,1));
	    
	    //分享达人
	    $member = D('Weixin/Index')->sharedaren(5, $this->userid);
	    $this->assign('sharedaren', $member);
	    
	    $work = D('Weixin/Index')->getWorkLight(2);
	    $this->assign('work', $work);
	    
	    $suipai = D('Weixin/Index')->getSuipaiLight(3);
	    $this->assign('suipai', $suipai);
	    
	    $stime=strtotime(date('Y-m-d 00:00:00'));
	    $etime=strtotime(date('Y-m-d 23:59:59'));
	    $where['userid']= array('EQ',$this->userid);
	    $where['create_time']=array(array('GT',$stime),array('LT',$etime),'and');
	    $where['action']=array('EQ','sign');
	    $mysign = 0;
	    $sign = getCount('UserDynamic',$where);
	    if($sign)
	    {
	        $mysign = $sign;
	    }
	    $this->assign('mysign', $mysign);
	    $this->assign('sysnum', $this->getsysmsgnum());
	    
	    $list = D('Weixin/index')->getLightCate();
	    $this->assign('list', $list);
	    
	    $this->assign('pageType', 'index');
	    $this->assign('nav', D('Weixin/index')->getNav(5));
	    $this->assign('activity', D('Weixin/index')->getActivity());
		$this->display();
	}
	
	/**
	 * 搜索
	 */
	public function search()
	{
	    $list = D('Weixin/index')->getLightCate();
	    $this->assign('list', $list);
	    $this->display();
	}

	/**
	 * 分类列表
	 */
	public function category()
	{
	    $index = D('Weixin/Index');
	    $category = $index->getCategory(0, true);
	    $this->assign('category', $category);
	    $this->display();
	}
	
	/**
	 * 达人列表
	 */
	public function member()
	{
	    $userModel=M('User');
	    $where['status']=array('EQ',true);
	    $count = $userModel->where($where)->count();
	    $page = $this->Pages($count, 10);
	    $list=$userModel
	    ->where($where)
	    ->field('userid,uavatar,openuid,nickname,worksnum,spnum,fansnum')
	    ->order('worksnum DESC')
	    ->limit($page->firstRow . ',' . $page->listRows)
	    ->select();
	    foreach($list as $key=>$value){
	        if(M('UserFans')->where(array('userid'=>$value['userid'],'myid'=>$this->userid,'status'=>true))->count()>0){
	            $list[$key]['isfans']=true;
	        }
	    }
	    
	    if($_GET['code'] == 'getPage')
	    {
	        if(count($list) > 0)
	        {
	            $this->success($list);
	        }
	        $this->error('fail');
	    }
	    
	    $this->assign("list", $list);
	    $this->display();
	}
	
	/**
	 * 全部评论列表
	 */
	public function msgList()
	{
	    $unionModel=new Model();
	    $type = I('get.type');
	    $id = I('get.id');
	    $msgid = I('get.msgid');
	    $this->assign('msgid', $msgid);
	    $where['infoid'] = $id;
	    $where['type'] = $type;
	    $where['parent_id'] = 0;//yf 0915
	    $count = M('UserMessage')->where($where)->count();
	    $this->assign('count', $count);
	    // $limitRows = 5; // 设置每页记录数
	    $limitRows = C('YF_PAGE');
	    $page = $this->Pages($count, $limitRows); 
	    $message=$unionModel
	    ->table(array(
	        C('DB_PREFIX').'user_message'=>'M',
	        C('DB_PREFIX').'user'=>'U',
	    )
	    )
	    ->where('M.userid=U.userid and M.status=true and M.infoid='.$id.' and M.type='.$type.' and M.parent_id=0')
	    ->field('M.create_time,M.content,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,M.id,M.touserid')
	    ->order('M.create_time DESC')
	    ->limit($page->firstRow . ',' . $page->listRows)
	    ->select();
	    
	    foreach($message as $k=>$v)
	    {
	        $child_temp = array();
	        $child_temp = $unionModel->table(C('DB_PREFIX').'user_message')->field('*')->where(array('parent_id' => $v['id']))->order('create_time ASC')->select();
	         
	        $message[$k]['childlist'] = array();
	        if($child_temp)
	        {
	            foreach($child_temp as $yk => $yv)
	            {
	                if($yv['userid'] == 0)
	                {
	                    $child_temp[$yk]['nickname'] = "烘焙圈";
	                    $child_temp[$yk]['openuid'] = 0;
	                }
	                else
	                {
	                    $yf_user = array();
	                    $yf_user = M('User')->field('openuid, nickname, uprovince, ucity, uavatar, ifrz')->where(array('userid'=>$yv['userid']))->find();
	                    $child_temp[$yk]['nickname'] = $yf_user['nickname'];
	                    $child_temp[$yk]['openuid'] = $yf_user['openuid'];
	                    $child_temp[$yk]['uprovince'] = $yf_user['uprovince'];
	                    $child_temp[$yk]['ucity'] = $yf_user['ucity'];
	                    $child_temp[$yk]['uavatar'] = $yf_user['uavatar'];
	                    $child_temp[$yk]['ifrz'] = $yf_user['ifrz'];
	                }
	                if($yv['touserid'] != 0)
	                {
	                    $child_temp[$yk]['touser'] = M('User')->field('userid,openuid,nickname')->where(array('userid'=>$yv['touserid']))->find();
	                }
	                else
	                {
	                    $child_temp[$yk]['touser']['nickname'] = "烘焙圈";
	                    $child_temp[$yk]['touser']['openuid'] = 0;
	                }
	            }
	            $message[$k]['childlist'] = $child_temp;
	        }
	        //$message[$k]['tempname'] = hy_substr($v['nickname'], 4, true);
	        $message[$k]['tempname'] = $v['nickname'];
	        $message[$k]['touser'] = array();
	        if(!empty($v['touserid']))
	        {
	            $message[$k]['touser']=M('User')->field('openuid,nickname')->where(array('userid'=>$v['touserid']))->find();
	        }
	        $message[$k]['create_time'] = format_time($v['create_time'], 'Y-m-d H-i');
	    }
	    unset($where['parent_id']);
	    
	    if($_GET['code'] == 'getPage')
	    {
	        if(count($message) > 0)
	        {
	            $this->success($message);
	        }
	        $this->error('fail');
	    }
	    
	    $this->assign('msglist', $message);
	    $this->assign('type', $type);
	    $this->assign('id', $id);
	    $this->display();
	}
	
	/**
	 * 评论
	 */
	public function comment()
	{
	    $post=I('post.');
        $post['userid'] = $this->userid;
        $this->isLogin(1);
        $type = $post['type'];
        if($type==1)
        {
            $table='Works';
            $map['workid']=$post['infoid'];         
        }
        elseif($type==4)
        {
            $table='Suipai';
            $map['id']=$post['infoid'];
        }
        elseif($type==7)
        {
            $table="Teachlist";
            $map['teachid']=$post['infoid'];
        }

        //当天第一次留言
        $stime=strtotime(date('Y-m-d 00:00:00'));
        $etime=strtotime(date('Y-m-d 23:59:59'));
        $where['userid']= array('EQ',$this->userid);
        $where['create_time']=array(array('GT',$stime),array('LT',$etime),'and');

        $todayMsgnum=M('UserMessage')->where($where)->count();
		$post['parent_id'] = isset($post['parent_id']) ? intval($post['parent_id']) : 0;
		$id = D('Weixin/Index')->sendMessage($post,$type);
        if($id){

            if($todayMsgnum<1){
                $userarr['upoint'] = array('exp','upoint+2');
                M('User')->where(array('userid'=>$this->userid))->save($userarr);
            }
            optMsgnum($table,$map,'add');

            //每天前20条留言有积分送
            $where['action']= array('EQ','message');
            $signnum=getCount('UserDynamic',$where);
           
             if($signnum <= 20){

               service('Passport')->userDynamic($this->userid,2,'评论留言获取2个积分',1,'message');
             }

            $unionModel=new Model();
             
            $info = $unionModel->table(array(
                C('DB_PREFIX').'user'=>'U',
                C('DB_PREFIX').'user_message'=>'S',
            ))
            ->where('U.userid=S.userid and S.id='.$id)
            ->field('U.nickname,U.uavatar,U.userid,S.id,S.create_time,S.content,S.touserid')
            ->find();
            $info['create_time'] = format_time($info['create_time']);//date('Y-m-d H:i', $info['create_time']);
            $this->success($info);
        }else{
            $error = D('Weixin/Index')->getError();
            $this->error($error ? $error : '留言失败,请稍候再试！');
        }
	}
	
	/**
	 * 回复评论
	 */
	public function replymsg(){
	    $this->isLogin(1);
	    $post=I('post.');
	    if(empty($post)){
	        $this->error('回复出错,请稍候再试!');
	    }
	    if($post['touserid'] == $this->userid){
	        $this->error('自己不能回复自己留言!');
	    }
	    $post['create_time']=time();
	    $post['userid']=$this->userid;
	    $post['parent_id'] = empty($post['parent_id']) ? 0 : intval($post['parent_id']);
	
	    $type=$post['type'];
	    if($type==1)
	    {
	        $table='Works';
	        $map['workid']=$post['infoid'];
	    }
	    elseif($type==4)
	    {
	        $table='Suipai';
	        $map['id']=$post['infoid'];
	    }
	    elseif($type==7)
	    {
	        $table='Teachlist';
	        $map['teachid']=$post['infoid'];
	    }
	
	    $id = D('Weixin/Index')->sendMessage($post,$post['type']);
	    if($id){
	        optMsgnum($table,$map,'add');
	        //每天前20条留言有积分送
	        $stime=strtotime(date('Y-m-d 00:00:00'));
	        $etime=strtotime(date('Y-m-d 23:59:59'));
	        $where['userid']= array('EQ',$this->userid);
	        $where['create_time']=array(array('GT',$stime),array('LT',$etime),'and');
	        $where['action']= array('EQ','message');
	        $signnum=getCount('UserDynamic',$where);
	        if($signnum <= 20){
	
	            service('Passport')->userDynamic($this->userid,2,'回复留言获取2个积分',1,'message');
	        }
	        $unionModel=new Model();
	         
	        $info = $unionModel->table(array(
	            C('DB_PREFIX').'user'=>'U',
	            C('DB_PREFIX').'user_message'=>'S',
	        ))
	        ->where('U.userid=S.userid and S.id='.$id)
	        ->field('U.nickname,U.uavatar,U.userid,S.id,S.content,S.touserid')
	        ->find();
            $info['touserName'] = M('User')->where(array('userid' => $info['touserid']))->getField('nickname');
	        $this->success($info);
	    }else{
	        $error = D('Weixin/Index')->getError();
	        $this->error($error ? $error : '回复失败,请稍候再试！');
	    }
	}
	
	/**
	 * 添加关注
	 */
	public function addfans(){
	    $this->isLogin(1);
	    $get=I('get.');
	    $get['myid']=$this->userid;
	    if($get['userid']==$this->userid){
	        $this->error('自己不能关注自己!');
	    }
	    if(D('Weixin/Index')->addfans($get)){
	        $this->success('关注成功');
	    }else{
	        $error = D('Weixin/Index')->getError();
	        $this->error($error ? $error : '关注失败,请稍候再试！');
	    }
	}
	
	/**
	 * 取消关注
	 */
	public function cancelfans(){
	    $this->isLogin(1);
	    $get=I('get.');
	    $get['myid']=$this->userid;
	    $fansModel=M('UserFans');
	    $where['myid']=$this->userid;
	    $where['userid']=$get['userid'];
	    $data['status']=false;
	    if($fansModel->where(array('myid'=>$get['myid'],'userid'=>$get['userid'], 'status'=>false))->count()>0){
	        $this->success('取消关注成功');
	        return false;
	    }
	    $result=$fansModel->where($where)->save($data);
	    if($result){
	        M('User')->where(array('userid'=>$get['myid']))->setDec('attentionnum',1);
	        M('User')->where(array('userid'=>$get['userid']))->setDec('fansnum',1);
	        $this->success('取消关注成功');
	    }else{
	        $this->error('取消操作失败');
	    }
	}
	
	/**
	 * 收藏作品操作
	 */
	public function addfavorite(){
	    $this->isLogin(1);
	    $get = I('get.');
	    $get['userid'] = $this->userid;
	    $workuid = M('Works')->where(array('workid'=>$get['infoid']))->getField('userid');
	    if($workuid == $this->userid){
	        $this->error('您不能收藏自己的作品!');
	    }
	    if(D('Weixin/Index')->addfavorite($get,1)){
	        $this->success(1);
	    }else{
	        $error = D('Weixin/Index')->getError();
	        $this->error($error ? $error : '收藏失败,请稍候再试！');
	    }
	}
	
	/**
	 * 取消收藏操作
	 */
	public function cancelfavorite(){
	    $this->isLogin(1);
	    $digModel=M('UserFavorite');
	    $get=I('get.');
	    $where['userid']=$this->userid;
	    $where['infoid']=$get['infoid'];
	    $data['status']=false;
	    $result=$digModel->where($where)->save($data);
	    if($result){
	        M('Works')->where(array('workid'=>$get['infoid']))->setDec('favoritenum',1);
	        $this->success(1);
	    }else{
	        $this->error('取消失败');
	    }
	}
	
	/**
	 * 点赞
	 */
	public function adddig(){
	    $this->isLogin(1);
	    $get = I('get.');
	    $get['userid'] = $this->userid;
	    $workuid = M('Works')->where(array('workid'=>$get['infoid']))->getField('userid');
	    if($workuid == $this->userid){
	        $this->error('您不能评价自己的作品!');
	    }
	    if(D('Weixin/Index')->adddig($get,1)){
	        if($get['digtype']==1){
	            $this->success(1);
	        }
	        elseif($get['digtype']==0){
	            $this->success('成功点评','do/canceldig/');
	        }
	    }else{
	        $error = D('Weixin/Index')->getError();
	        $this->error($error ? $error : '点赞失败,请稍候再试！');
	    }
	}
	
	/**
	 * 取消点赞
	 */
	public function canceldig(){
	    $this->isLogin(1);
	    $digModel=M('Dig');
	    $get=I('get.');
	    $where['userid']=$this->userid;
	    $where['infoid']=$get['infoid'];
	    $diginfo=$digModel->field('digtype,create_time')->where(array('userid'=>$this->userid,'infoid'=>$get['infoid']))->find();
// 	    $btwtime=time()-$diginfo['create_time'];
// 	    if($btwtime<30){
// 	        $this->error(30-$btwtime.'秒后再试');
// 	        die;
// 	    }
	    if($diginfo['digtype']==1){
	        M('Works')->where(array('workid'=>$get['infoid']))->setDec('goodnum',1);
	        $digModel->where($where)->save(array('status'=>false));
	        $this->success(1);
	    }elseif($diginfo['digtype']==0){
	        M('Works')->where(array('workid'=>$get['infoid']))->setDec('badnum',1);
	        $digModel->where($where)->save(array('status'=>false));
	        $this->success('成功取消点评','do/adddig/');
	    }
	}
}

?>